﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using OneMake.DataAccess;
using OneMake.Excel;
using OneMake.Model;
using OneMake.Winforms;

namespace OneMake.Winforms
{
    public partial class LoginForm : Form
    {
        private DataHelper dataHelper = new DataHelper();

        public LoginForm()
        {
            InitializeComponent();

            cbNetwork.Items.Add("内网");
            cbNetwork.Items.Add("外网");
            cbNetwork.SelectedIndex = 0;

            lbVersion.Text = "版本：" + System.Reflection.Assembly.GetEntryAssembly().GetName().Version;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            bool result = false;
            int employeeid = 0;

            if ((String)cbNetwork.SelectedItem == "外网")
                UserSession.Network = "Internet";
            else
                UserSession.Network = "";

            using (var db = new PetaPoco.Database("mysql" + UserSession.Network))
            {
               try
                {
                    if (editUserName.Text.Trim() == "")
                    {
                        MessageBox.Show("请输入工号！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        editUserName.Focus();
                        return;
                    }
                    if (editPassword.Text.Trim() == "")
                    {
                        MessageBox.Show("请输入密码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        editPassword.Focus();
                        return;
                    }

                    foreach (employee_info emp in db.Query<employee_info>("select * from employee_info"))
                    {
                        if (emp.employee_no == editUserName.Text && emp.password == editPassword.Text)
                        {
                            DialogResult = DialogResult.OK;

                            UserSession.Logined = true;
                            UserSession.UserName = editUserName.Text;
                            UserSession.PassWord = editPassword.Text;
                            UserSession.EmployeeId = emp.employeeid;

                            employeeid = emp.employeeid;
                            result = true;
                            break;
                        }
                    }

                    if (!result)
                    {
                        MessageBox.Show("工号或密码错误，请重新输入！", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        db.Execute("INSERT INTO log_operation(log_datetime, log_level, log_code, log_msg, employeeid) values(now(), @0, @1, @2, @3)",
                            0, 0, editUserName.Text + "登录成功", employeeid);
                    }
                }
                catch (Exception ex)
                {
                    //System.Diagnostics.Debug.Print(ex.Message);
                    //server=110.86.16.202;port=43306;uid=web;pwd=web;database=onemake;
                    string host = "";
                    string[] segs = db.Connection.ConnectionString.Split(';');
                    foreach (string seg in segs)
                    {
                        string[] paramters = seg.Split('=');
                        if (paramters.Length == 2)
                        {
                            if (paramters[0].ToUpper() == "SERVER")
                            {
                                host = paramters[1];
                            }
                        }
                    }
                    MessageBox.Show("无法连接服务器"+host+"，请确认当前网络连接是否正常！\n"+ex.Message, "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
        }

        private void editPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin_Click(sender, e);
            }
        }

        private void editUserName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                editPassword.Focus();
            }
        }
    }
}
